<template>
  <Table
    ref="tableRef"
    :url="URL_PAGEQUERYSTATIONTASKTRANSFERPRICE"
    :columns="data.columns"
    :handles="data.handles"
    :toolbar="data.toolbar"
    :query="data.query"
  ></Table>
</template>

<script lang="ts" setup name="priceTimeList">
import Table from '@/components/table/index.vue';
import {
  URL_PAGEQUERYSTATIONTASKTRANSFERPRICE,
  delStationTaskTransferPrice,
} from '@/service/apis/pile';
import { queryDicByParent } from '@/service/apis/api';

const router = useRouter();

const tableRef = ref();

const emit = defineEmits(['get-update']);
onActivated(() => {
  emit('get-update', (flag: boolean) => {
    if (flag) {
      tableRef.value?.getData();
    }
  });
});

queryDicByParent({
  listDtos: ['stationTaskTransferPriceStatus', 'priceSet'],
}).then((res: any) => {
  if (res) {
    data.columns[1].dic = [...res.priceSet];
    data.columns[2].dic = [...res.stationTaskTransferPriceStatus];
    data.query[1].props.options = [...res.stationTaskTransferPriceStatus];
  }
});

const data = reactive({
  columns: [
    {
      prop: 'taskName',
      label: '任务名称',
    },
    {
      prop: 'priceSet',
      label: '电桩价格类型',
      dic: [] as any,
    },
    {
      prop: 'status',
      label: '任务状态',
      dic: [] as any,
      type: Number,
    },
    {
      prop: 'nextTime',
      label: '生效时间',
    },
    {
      prop: 'updateBy',
      label: '修改人',
    },
    {
      prop: 'updateTime',
      label: '修改时间',
    },
  ],
  handles: [
    {
      label: '调价结果 ',
      handle: (row: any) => {
        console.log('应用电桩 ---', row);
        router.push({
          name: 'priceTimeDetail',
          query: { id: row.id },
        });
      },
    },
    {
      label: '修改',
      permission: 'priceTimeList.edit',
      disabledFun: (row: any) => {
        return row.status !== 1;
      },
      handle: (row: any) => {
        console.log('编辑---', row);
        router.push({
          name: 'priceTimeEdit',
          query: { id: row.id },
        });
      },
    },
    {
      label: '删除',
      color: '#FA3939',
      permission: 'priceTimeList.delete',
      disabledFun: (row: any) => {
        return row.status !== 1;
      },
      handle: (row: any) => {
        ElMessageBox.confirm('任务删除后，即不再执行调价操作？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          delStationTaskTransferPrice({
            id: row.id,
          }).then((res: any) => {
            if (res) {
              tableRef.value.getData();
            }
          });
        });
      },
    },
  ],
  toolbar: [
    {
      label: '新增',
      permission: 'priceTimeList.add',
      icon: 'icon-add',
      type: 'primary',
      handle: (row: any) => {
        router.push({
          name: 'priceTimeAdd',
        });
      },
    },
  ],
  query: [
    {
      label: '任务名称',
      prop: 'taskName',
      comName: 'input',
      props: {
        placeholder: '请输入任务名称',
      },
    },
    {
      label: '任务状态',
      prop: 'status',
      comName: 'select',
      props: {
        placeholder: '请选择任务状态',
        change: (value: any) => {
          console.log(value);
        },
        options: [] as any,
      },
    },
  ],
});
</script>